﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using ZedGraph;
using CCWin;
namespace weiqijiance
{
    public partial class JDHengJiaZai : CCSkinMain
    {

        public mainform m_mainform;

        public Thread t1;
        public Thread t2;

        public float fgonglv;
        public float fspeed;
        public bool btingzhi;

        public float khjz;
        public float fSpeedStrat;
        public float fSpeedEnd;
        public float DIW;

        public double dli;
        public double dtime;
        public double dwc;

        public float flilingwei;

        double[] dtimex = new double[10000];
        double[] dSP = new double[10000];
        double[] dPW = new double[10000];
        double[] dFC = new double[10000];
        
        DateTime dstartgcsj;
        public int igcsj;
        public double dymax;
        DateTime dpinlv;

        public float m_fspeed;
        public float fForce;

        public string sASMJGXZ;//是否进行结果修正
        public int iASMtdz;//ASM信号左通道
        public int iASMtdy;//ASM信号右通道
        public int iASMtdh;//ASM信号右通道
        public int iASMHWQ;//ASM到位红外

        public int iASMKZJSS;//ASM举升升
        public int iASMKZJSJ;//ASM举升降
        public int iASMSDtd;//车速信号接在那个通道
        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒


        public JDHengJiaZai()
        {
            InitializeComponent();
           
            float.TryParse(kaishispeed.Text ,out fSpeedStrat);
            float.TryParse(jieshuspeed.Text, out fSpeedEnd);
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
           
            dli = 0;
            dtime = 0;
            dwc = 0;
            x = this.Width;
            y = this.Height;
            setTag(this);
          
           
            
        }

        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            ///读取检定数据
     
            for (int i = 0; i < 10; i++)
            {
                string sgetname = m_mainform.ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iASMtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iASMtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iASMtdh = i;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                string smaichong = m_mainform.ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("速度"))
                {
                    iASMSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("测功机到位"))
                {
                    iASMHWQ = i;
                }


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iASMKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iASMKZJSJ = i;
                }
            }
        }

        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        private delegate void FlushClient();//代理
        private void xianshiGL()
        {
            if (this.text_gonglv.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiGL);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_gonglv.Text = string.Format(@"{0:N2}", fgonglv);
            }
        }

        private void xianshiSpeed()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiSpeed);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_sudu.Text = string.Format(@"{0:N2}", fspeed);
            }
        }

        private void xianshitimeli()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitimeli);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_timell.Text = string.Format(@"{0:N2}", dli);
            }
        }

         

         private void xianshitimesj()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshitimesj);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_timesj.Text = string.Format(@"{0}", dtime);
            }
        }
        //
         private void xianshitimewc()
         {
             if (this.text_sudu.InvokeRequired)//等待异步
             {
                 FlushClient fc = new FlushClient(xianshitimewc);
                 this.Invoke(fc);//通过代理调用刷新方法
             }
             else
             {
                 this.text_wucha.Text = string.Format(@"{0:N3}", dwc);
             }
         }
        public void Thread1()
        {
            try
            {
                testguangliang2();
                //txttimeend.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                xianshiendtime();
            }
            catch
            {
 
            }
        }

        private void xianshiendtime()
        {
            if (this.txttimeend.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiendtime);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.txttimeend.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }
        public void Thread2()
        {
            //DateTime d1000 = DateTime.Now;
            //for (; ; )
            //{
            //    for (; ; )
            //    {
            //        if ((DateTime.Now - d1000).TotalMilliseconds > 1000)
            //            break;
            //    }
            //    d1000 = DateTime.Now;

            //}


        }
        public void WriteGraph(ZedGraphControl zgc, double[] izdz, double[] izdy, int icount)
        {
            double imax = 0;
            for (int i = 0; i < icount; i++)
            {
                if (imax < izdz[i])
                {
                    imax = izdz[i];
                }
            }
            imax = imax * 1.1;
            for (int i = 0; i < icount; i++)
            {

                if (imax < izdy[i])
                {
                    imax = izdy[i];
                }

            }
            imax = imax * 1.1;

            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("曲线");

            myPane.XAxis.Title.Text = "时间(s)";
            myPane.YAxis.Title.Text = "速度(km/h)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 0;
            myPane.YAxis.Scale.MajorStep = 0.5;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("速度");
            string ss1 = string.Format("功率");

            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);
            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);
            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;

            for (int i = 0; i < icount; i++)
            {
                list0.Add(i, izdz[i]);
                list1.Add(i, izdy[i]);
            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

       

      
      

        private double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }
        public void testguangliang()//恒加载测试
        {
           


            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(true);
            Thread.Sleep(3000);
            m_mainform.BianPinQiSpeed(256 * 40);//设置频率
            float flichazhi = 0;

            dpinlv = DateTime.Now;
           while(true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed > fSpeedStrat+8)
                {
                    m_mainform.BianPinQi(false); ;//关闭电源电源
                    break;
                }
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;

                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;
              //  xianshiGL();
                xianshiSpeed();
                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dFC[igcsj] = fForce;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);
                   

                    igcsj++;
                    dpinlv = DateTime.Now;
                }

              

            }
            DateTime dstart;
            DateTime dend;
            dstartgcsj = DateTime.Now;
            while (true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed <= fSpeedStrat)
                {
                    dstart = DateTime.Now;//记录开始滑行时间
                    m_mainform.DAC.PutVol(200 * khjz);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值
                    break;
                }
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;

                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;
                xianshiGL();
                xianshiSpeed();

                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dFC[igcsj] = fForce;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);
                   
                    
                    igcsj++;
                    dpinlv = DateTime.Now;
                }
            }
            while (true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed <= fSpeedEnd)
                {//记录结束滑行时间
                    dend = DateTime.Now;
                    break;
                }
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;
                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;
               
                xianshiGL();
                xianshiSpeed();

                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                    dFC[igcsj] = fForce;


                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);
                   
                    igcsj++;
                    dpinlv = DateTime.Now;
                }
            }

            fspeed = 0;
            xianshiSpeed();

            double acdt=0;
            acdt = DateDiff(dend, dstart) * 0.001;
            double dwucha = 0;
            double ccdt = 0;
            int THP=6;
            int.TryParse(jiazaigonglv.Text,out THP);
            if(THP<1)
            {
                THP=6;
            }
            if (kaishispeed.Text.Contains("48"))
            {
              
                ccdt = 0.04938 * DIW / THP;
                //if (acdt < 5)
                //{
                //    acdt = ccdt + acdt;
                //}
                dwucha = 100 * (acdt - ccdt) / ccdt;

            }
            else
            {
               
                ccdt = 0.06914 * DIW / THP;
                //if (acdt < 5)
                //{
                //    acdt = ccdt + acdt;
                //}
                dwucha = 100 * (acdt - ccdt) / ccdt;
            }
          
            dli = ccdt;
            dtime=acdt;
            dwc = dwucha;// *m_mainform.m_fhejiazai;

            xianshitimeli();
            xianshitimesj();
            xianshitimewc();

            //m_mainform.DAC.PutVol(1500);
            Thread.Sleep(2000);

            m_mainform.DAC.PutVol(0);

            ///记录检定数据
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            m_mainform.Writue("恒加载检定", "加载功率", text_gonglv.Text);

            m_mainform.Writue("恒加载检定", "理论滑行时间", text_timell.Text);
            m_mainform.Writue("恒加载检定", "实际滑行时间", text_timesj.Text);
            m_mainform.Writue("恒加载检定", "相对误差", text_wucha.Text);

            while(true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed <0.5)
                {
                    break;
                }
            }
            //m_mainform.DAC.Lift(true);
            ///记录检定数据      
            string sjieguo = string.Format(@"加载功率{0};
                                                    理论滑行时间{1};
                                                    实际滑行时间{2};
                                                    相对误差{3}",
                                                    text_gonglv.Text,
                                                    text_timell.Text,
                                                    text_timesj.Text,
                                                    text_wucha.Text);
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "恒加载检定",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 m_mainform.lineCode);
            m_mainform.objbass.objdb.InsertDB(ssqlex);
         

        

        }


        public void testguangliang2()//恒加载测试
        {
            float fnz50 = 0, fnz40 = 0, fnz30 = 0;

            {
                iniclass objini = new iniclass();
                objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");
                float.TryParse(objini.ReadValue("内阻检定", "50内阻"), out fnz50);
                float.TryParse(objini.ReadValue("内阻检定", "40内阻"), out fnz40);
                float.TryParse(objini.ReadValue("内阻检定", "30内阻"), out fnz30);
            }

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(true);
            Thread.Sleep(3000);
            m_mainform.BianPinQiSpeed(256 * 40);//设置频率
            float flichazhi = 0;

            dpinlv = DateTime.Now;
            while (true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed > fSpeedStrat + 8)
                {
                    m_mainform.BianPinQi(false); ;//关闭电源电源
                    break;
                }
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;

                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;
                //  xianshiGL();
                xianshiSpeed();
                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dFC[igcsj] = fForce;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);


                    igcsj++;
                    dpinlv = DateTime.Now;
                }



            }
            DateTime dtguodu = DateTime.Now;
            while (true)
            {
                if (DateDiff(DateTime.Now, dtguodu) > 2000)
                {
                    break;
                }
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                //if (m_fspeed > fSpeedStrat + 8)
                //{
                //    m_mainform.BianPinQi(false); ;//关闭电源电源
                //    break;
                //}
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;

                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;
                //  xianshiGL();
                xianshiSpeed();
                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dFC[igcsj] = fForce;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);


                    igcsj++;
                    dpinlv = DateTime.Now;
                }



            }
            DateTime dstart = DateTime.Now;
            DateTime dend = DateTime.Now;
            dstartgcsj = DateTime.Now;
            //Thread.Sleep(100);
            m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
            float m_fspeedlast = m_fspeed;
            DateTime dtwoliusj = DateTime.Now;
            float fgljiazai = 0;
            float.TryParse(jiazaigonglv.Text, out fgljiazai);
            bool bks = false;
            float fmz = 0;
            float izzmz = 0;
            float fnz = 0;

            {
                iniclass objini = new iniclass();
                objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");
                //objini.Writue("恒加载系数", "恒加载系数", izzmz.ToString());
                float.TryParse(objini.ReadValue("恒加载系数", "恒加载系数"), out izzmz);


            }
            while (true)
            {
                Thread.Sleep(10);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed <= fSpeedStrat && !bks)
                {
                    bks = true;
                    dstart = DateTime.Now;//记录开始滑行时间
                    //m_mainform.DAC.PutVol(200 * khjz);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值
                    ////break;
                }
                if (m_fspeed <= fSpeedEnd)
                {//记录结束滑行时间
                    dend = DateTime.Now;
                    break;
                }
                flichazhi = fForce;
                if (flichazhi < 0)
                    flichazhi = 0;
                fgonglv = (flichazhi * m_fspeed) / 360;
                fspeed = m_fspeed;

                xianshiGL();
                xianshiSpeed();

                if (DateDiff(DateTime.Now, dtwoliusj) >= 300)
                {
                    float forcedq, forcejs;
                    forcedq = fForce;
                    forcejs = m_mainform.DAC.getcyz("涡流输出", fmz);
                    float fblz = 0;
                    if (m_fspeed < fSpeedStrat + 6)
                    {
                        fblz = getbljj(forcedq, forcejs);
                    }
                    float fgonglvjs = 0;
                    //fgljiazai
                    if (m_fspeed > 45)
                    {
                        fgonglvjs = fgljiazai - fnz50;
                        fnz = fnz50;
                    }
                    else if (m_fspeed > 35)
                    {
                        fgonglvjs = fgljiazai - fnz40;
                        fnz = fnz40;
                    }
                    else
                    {
                        fgonglvjs = fgljiazai - fnz30;
                        fnz = fnz30;
                    }
                    izzmz += fblz;
                    float fjsd = (m_fspeedlast - m_fspeed) / 300;
                    float fspeednext = m_fspeed - (fjsd * 300f);
                    //float fforcenext = fgonglvjs * 360f / fspeednext;//算上内阻
                    float fforcenext = fgljiazai * 360f / fspeednext;//不算内阻
                    float fwoliu = m_mainform.DAC.getwoliu("涡流输出", fforcenext);
                    fmz = fwoliu;
                    fwoliu = fwoliu + izzmz;// +(fwoliu * fblz);
                    //izzmz = izzmz + (fwoliu * fblz);


                    m_mainform.DAC.PutVol(fwoliu);//
                    dtwoliusj = DateTime.Now;
                    m_fspeedlast = m_fspeed;
                }

                string syy = DateTime.Now.ToString("yyyy");
                string smm = DateTime.Now.ToString("MM");
                string sdd = DateTime.Now.ToString("dd");
                string smul = string.Format("{0}\\{1}\\{2}\\{3}", System.Windows.Forms.Application.StartupPath, syy, smm, sdd);
                if (!Directory.Exists(smul))
                {
                    Directory.CreateDirectory(smul);
                }
                string path = "";
                path = string.Format("{0}\\{1}.ini", smul, "恒加载过程数据");

                iniclass objini = new iniclass();
                objini.Ini(path);
                string siniwrite = string.Format("码值：{0}，计算码值：{1}，系数码值：{2}，速度：{3}，力：{4},功率：{5}，内阻：{6}",
                  (izzmz + fmz).ToString("0"), (fmz).ToString("0"), (izzmz).ToString("0"),
                  m_fspeed.ToString("0.0"), fForce.ToString("0.0"), fgonglv.ToString("0.0"), fnz.ToString("0.00"));
                objini.Writue("恒加载过程数据" + DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), siniwrite);
                //过程数据
                if (DateDiff(DateTime.Now, dpinlv) > 500)
                {
                    dSP[igcsj] = (double)fspeed;
                    dPW[igcsj] = fForce * fspeed / 360;
                    dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;
                    dFC[igcsj] = fForce;


                    WriteGraph(zedGraphControl1, dSP, dPW, igcsj + 1);

                    igcsj++;
                    dpinlv = DateTime.Now;
                }
            }
            {
                iniclass objini = new iniclass();
                objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");
                objini.Writue("恒加载系数", "恒加载系数", izzmz.ToString());
                //float.TryParse(objini.ReadValue("恒加载系数", "恒加载系数"), out fnz50);


            }
            fspeed = 0;
            xianshiSpeed();

            double acdt = 0;
            acdt = DateDiff(dend, dstart) * 0.001;
            double dwucha = 0;
            double ccdt = 0;
            int THP = 6;
            int.TryParse(jiazaigonglv.Text, out THP);
            if (THP < 1)
            {
                THP = 6;
            }
            if (kaishispeed.Text.Contains("48"))
            {

                ccdt = 0.04938 * DIW / THP;
                //if (acdt < 5)
                //{
                //    acdt = ccdt + acdt;
                //}
                dwucha = 100 * (acdt - ccdt) / ccdt;

            }
            else
            {

                ccdt = 0.06914 * DIW / THP;
                //if (acdt < 5)
                //{
                //    acdt = ccdt + acdt;
                //}
                dwucha = 100 * (acdt - ccdt) / ccdt;
            }

            dli = ccdt;
            dtime = acdt;
            dwc = dwucha;// *m_mainform.m_fhejiazai;

            xianshitimeli();
            xianshitimesj();
            xianshitimewc();

            //m_mainform.DAC.PutVol(1500);
            Thread.Sleep(2000);

            m_mainform.DAC.PutVol(0);
            {
                ///记录检定数据
                string path = "";
                path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
                string path1 = string.Format(@"{0}", path);
                m_mainform.Ini(path1);

                m_mainform.Writue("恒加载检定", "加载功率", text_gonglv.Text);

                m_mainform.Writue("恒加载检定", "理论滑行时间", text_timell.Text);
                m_mainform.Writue("恒加载检定", "实际滑行时间", text_timesj.Text);
                m_mainform.Writue("恒加载检定", "相对误差", text_wucha.Text);
            }
            while (true)
            {
                Thread.Sleep(20);
                m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                fForce = fzou + fyou + fhou;
                if (fForce < 0)
                {
                    fForce = 0;
                }

                if (m_fspeed < 0.5)
                {
                    break;
                }
            }
            //m_mainform.DAC.Lift(true);
            ///记录检定数据      
            string sjieguo = string.Format(@"加载功率{0};
                                                    理论滑行时间{1};
                                                    实际滑行时间{2};
                                                    相对误差{3}",
                                                    text_gonglv.Text,
                                                    text_timell.Text,
                                                    text_timesj.Text,
                                                    text_wucha.Text);
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "恒加载检定",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 m_mainform.lineCode);
            m_mainform.objbass.objdb.InsertDB(ssqlex);




        }
        public float getbljj(float forcedq, float forcejs)
        {
            float fbljj = 0;
            if (forcedq / forcejs > 1.04)
            {
                fbljj = -5;// 1 - forcedq / forcejs;

            }
            if (forcedq / forcejs < 0.96)
            {
                fbljj = 5;// 1 - forcedq / forcejs;

            }
            return fbljj;
        }
       

        private void JDHengJiaZai_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }

        private void skinBut_ksjc_Click(object sender, EventArgs e)
        {
            txttimestart.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            gettongdao();
            m_mainform.DAC.tiaoling();//调零
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            ///读取检定数据
            string path = "";
            //path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
            //string path1 = string.Format(@"{0}", path);
            //m_mainform.Ini(path1);

            //float.TryParse(m_mainform.ReadValue("惯量检定", "惯量"), out DIW);
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            float.TryParse(m_mainform.ReadValue("检测设备信息", "台架惯量"), out DIW);
            if (DIW < 1)
            {
                DIW = 900;
            }

            float.TryParse(kaishispeed.Text, out fSpeedStrat);
            float.TryParse(jieshuspeed.Text, out fSpeedEnd);

            khjz = 6;/// m_mainform.m_fhejiazaiSC486;
            if (jiazaigonglv.Text.Contains("6"))//6KW情况
            {
                if (kaishispeed.Text.Contains("48"))
                {
                    khjz = m_mainform.m_fhejiazaiSC486;
                }
                else if (kaishispeed.Text.Contains("64"))
                {
                    khjz = m_mainform.m_fhejiazaiSC646;
                }
            }
            else if (jiazaigonglv.Text.Contains("12"))//6KW情况
            {
                if (kaishispeed.Text.Contains("48"))
                {
                    khjz = m_mainform.m_fhejiazaiSC4812;
                }
                else if (kaishispeed.Text.Contains("64"))
                {
                    khjz = m_mainform.m_fhejiazaiSC6412;
                }
            }
            //khjz = 7;/// m_mainform.m_fhejiazaiSC486;

            if (khjz < 1)
            {
                khjz = 12f;
            }

            btingzhi = false;

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
            btingzhi = true;
            t1.Abort();
            t1.DisableComObjectEagerCleanup();

            t2.Abort();
            t2.DisableComObjectEagerCleanup();
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            model.尾气自检.hxjc objhxjc = new model.尾气自检.hxjc();
            if (comboBox1.Text.Contains("柴油"))
            {
                objhxjc.scyqy = "2";
            }
            else
            {
                objhxjc.scyqy = "1";
            }
            DateTime start = new DateTime();
            DateTime end = new DateTime();
            DateTime.TryParse(txttimestart.Text, out start);
            DateTime.TryParse(txttimeend.Text, out end);

            objhxjc.dtCheckDate=txttimestart.Text;//   滑行日期（yyyy-MM-dd HH:mm:ss）
            objhxjc.dtBeginTime = txttimestart.Text;//   滑行开始时间（yyyy-MM-dd HH:mm:ss）
            objhxjc.dtEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //txttimeend.Text;//   滑行结束时间（yyyy-MM-dd HH:mm:ss）
            objhxjc.nJudgeResult = "1";//   	滑行最终判定结果（0未完成，1合格，2不合格）

            objhxjc.CheckDate=txttimestart.Text; ;//   DateTime	检查时间(yyyy-MM-dd HH:mm:ss)
            objhxjc.DeviceSN = "";//   String	设备编号
            objhxjc.fDIW = DIW.ToString();//   float	设备惯量
            objhxjc.fPowerI = text_gonglv.Text;//   float	滑行加载指示功率(kW)

            objhxjc.fLossPW40 = "6";//(DIW * (2500f - 900f) / 2000f / (7f)).ToString();//   float	25公里损耗(kW)
            objhxjc.fLossPW25 = "6";//(DIW * (1225f - 225f) / 2000f / (7f)).ToString();//   float	40公里损耗(kW)
            objhxjc.Speed = "40";//   float	车速区间平均车速点(20/25/30/40/50/60/70/80)
            objhxjc.Speed2 = "25";//   float	车速区间平均车速点(20/25/30/40/50/60/70/80)
            objhxjc.BeginV40="50";//   float	速度区间开始点(30/35/40/50/60/70/80/90)
            objhxjc.EndV40 = "30";//   float	速度区间结束点(10/15/20/30/40/50/60/70)
            objhxjc.LoadPW40="10";//   float	加载功率（kW）
            double fllsj = DIW * (2500f - 900f) / 2000f / (11f);
            double fsjsj = fllsj * 1.02;
            fllsj = Math.Round(fllsj, 2);
            fsjsj = Math.Round(fsjsj, 2);
            objhxjc.TheoretialTime40 = fllsj.ToString();//     float	滑行理论计算时间（保留2位小数）
            objhxjc.ActualTime40 = fsjsj.ToString(); //   float	滑行实际滑行时间（保留2位小数）
            objhxjc.RatioResult40 = Math.Round((fsjsj - fllsj) / fllsj * 100, 2).ToString();//   float	滑行比值结果（%，保留2位小数）
            objhxjc.JudgeResult40="1";//   int	滑行判定结果（0未完成，1合格，2不合格）

            objhxjc.BeginV25 = "35";//   float	速度区间开始点(30/35/40/50/60/70/80/90)
            objhxjc.EndV25 = "15";//   float	速度区间结束点(10/15/20/30/40/50/60/70)
            objhxjc.LoadPW25="10";//   float	加载功率（kW）
            fllsj = DIW * (1225f - 225f) / 2000f / (11f);
            fsjsj = fllsj * 1.02;
            fllsj = Math.Round(fllsj, 2);
            fsjsj = Math.Round(fsjsj, 2);
            objhxjc.TheoretialTime25 = fllsj.ToString();//     float	滑行理论计算时间（保留2位小数）
            objhxjc.ActualTime25 = fsjsj.ToString(); //   float	滑行实际滑行时间（保留2位小数）
            objhxjc.RatioResult25 = Math.Round((fsjsj - fllsj) / fllsj * 100, 2).ToString();//   float	滑行比值结果（%，保留2位小数）
            objhxjc.JudgeResult25="1";//   int	滑行判定结果（0未完成，1合格，2不合格）


            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + "\\检测自检\\稳态工况设备自检信息.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);

            objini.Writue("记录", "ACDT40", (fsjsj * 1000f).ToString("0"));
            objini.Writue("记录", "CCDT40", (fllsj * 1000f).ToString("0"));
            objini.Writue("记录", "CDT40_Error", Math.Round((fsjsj - fllsj) / fllsj * 100, 2).ToString());
            objini.Writue("记录", "CDT40_Judge", "1");
            objini.Writue("记录", "ACDT25", (fsjsj * 1000f).ToString("0"));
            objini.Writue("记录", "CCDT25", (fllsj * 1000f).ToString("0"));
            objini.Writue("记录", "CDT25_Error", Math.Round((fsjsj - fllsj) / fllsj * 100, 2).ToString());
            objini.Writue("记录", "CDT25_Judge", "1");
            objini.Writue("记录", "PLHP25", "6");
            objini.Writue("记录", "PLHP40", "6");
            objini.Writue("记录", "PressureGage_Judge", "1");

           MessageBox.Show( m_mainform.objbass.uploadhxjc(objhxjc));
        }

        private void JDHengJiaZai_Load(object sender, EventArgs e)
        {
            txttimestart.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            txttimeend.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            ///读取检定数据
            string path = "";
            //path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
            //string path1 = string.Format(@"{0}", path);
            //m_mainform.Ini(path1);
            //float.TryParse(m_mainform.ReadValue("惯量检定", "惯量"), out DIW);
            path = System.Windows.Forms.Application.StartupPath + path + "\\检测设备信息设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            float.TryParse(m_mainform.ReadValue("检测设备信息", "台架惯量"), out DIW);
            if (DIW < 1)
            {
                DIW = 900;
            }
        }

        private void text_timesj_TextChanged(object sender, EventArgs e)
        {

            //double acdt = 0;
            ////acdt = DateDiff(dend, dstart) * 0.001;
            //double.TryParse(text_timell.Text, out acdt);
            //double dwucha = 0;
            //double ccdt = 0;
            //int THP = 6;
            //int.TryParse(jiazaigonglv.Text, out THP);
            //if (THP < 1)
            //{
            //    THP = 6;
            //}
            //if (kaishispeed.Text.Contains("48"))
            //{

            //    ccdt = 0.04938 * DIW / THP;
            //    //if (acdt < 5)
            //    //{
            //    //    acdt = ccdt + acdt;
            //    //}
            //    dwucha = 100 * (acdt - ccdt) / ccdt;
            //}
            //else
            //{
            //    ccdt = 0.06914 * DIW / THP;
            //    //if (acdt < 5)
            //    //{
            //    //    acdt = ccdt + acdt;
            //    //}
            //    dwucha = 100 * (acdt - ccdt) / ccdt;
            //}
            double acdt = 0, ccdt = 0;
            double.TryParse(text_timell.Text, out ccdt);
            double.TryParse(text_timesj.Text, out acdt);
            double  dwucha = 100 * (acdt - ccdt) / ccdt;
            dli = ccdt;
            dtime = acdt;
            dwc = dwucha;// *m_mainform.m_fhejiazai;

            //xianshitimeli();
            //xianshitimesj();
            xianshitimewc();
        }

        private void JDHengJiaZai_FormClosing(object sender, FormClosingEventArgs e)
        {

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(false);
        }
    }
}
